//
//  HWVideoViewController.m
//  JWC
//
//  Created by lanou on 16/4/6.
//  Copyright © 2016年 陈帅. All rights reserved.
//

#import "HWVideoViewController.h"
#import "HWRecommentCell.h"
#import "HWEssenceTool.h"
#import "MJRefresh.h"
#import "HWCommentViewController.h"
#import "HWContentVideoView.h"
#import <AVFoundation/AVFoundation.h>
#import "KRVideoPlayerController.h"

static NSString *cellID = @"new";
@interface HWVideoViewController ()

@property (nonatomic, strong)HWEssenceTool *tool;
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, strong)KRVideoPlayerController *videoController;
// 当前页码
@property (nonatomic, assign)NSInteger page;
// 当加载下一页时需要的参数
@property (nonatomic, assign)NSInteger np;

@end

@implementation HWVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setRefresh];
    [self settableView];
}

#pragma mark --- 添加播放按钮通知 ---
- (void)setupPlayBtnNotification
{
    
}

#pragma mark --- 设置刷新控件 ---
-(void)setRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
    
    //自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    
}

// 加载新数据
- (void)getNewData
{
    // 清空
    self.page = 0;
    [self.modelArray removeAllObjects];
    // 这两个宏一定成对出现，先weak再strong.可以很好的管理Block内部对self的引用
    @weakify(self)
    [self.tool getDataWithType:TopicTypeVideo style:@"jingxuan" block:^(id json, NSInteger param) {
        @strongify(self)
        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        self.np = (NSInteger)param;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
}

// 加载更多数据
- (void)getMoreData
{
    // 计算页码
    NSInteger page = self.page + 1;
    @weakify(self)
    [self.tool getDataWithNp:self.np type:TopicTypeVideo style:@"jingxuan" block:^(id json, NSInteger param) {
        @strongify(self)
        for (HWRecommendModel *model in json)
        {
            HWFrame *frame = [[HWFrame alloc]init];
            frame.model = model;
            [self.modelArray addObject:frame];
        }
        [self.tableView reloadData];
        self.page = page;
        self.np = param;
        [self.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark --- 设置tableView ---
-(void)settableView{
    self.view.backgroundColor = BackgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HWRecommentCell" bundle:nil] forCellReuseIdentifier:cellID];
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWFrame *frame = self.modelArray[indexPath.row];
    HWCommentViewController * commentVC = [[HWCommentViewController alloc]init];
    commentVC.frame = frame;
    
    [self.navigationController pushViewController:commentVC animated:YES];
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWRecommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.topFrame = self.modelArray[indexPath.row];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //计算文字高度
    HWFrame *frame = self.modelArray[indexPath.row];
    return frame.cellHeight;
}

#pragma mark - getter and setter

- (HWEssenceTool *)tool{
    if (_tool == nil) {
        _tool = [[HWEssenceTool alloc]init];
    }
    return _tool;
}

- (NSMutableArray *)modelArray{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
        
    }
    return _modelArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
